package de.ventu.containers
{
	import com.mh.containers.FlowBox;
	
	import de.ventu.controls.ImageObject;
	import de.ventu.view.FullImageScreen;
	import de.ventu.view.SplitImageScreen;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	
	public class ImageFlowBox extends FlowBox
	{
		private var _images:ArrayCollection;
		private var _application:Object;
		private var _fullImageScreen:FullImageScreen
		
		public var lastSelectedImageUrl:String;
		public var currentFullscreenImage:ImageObject;
		
		public function ImageFlowBox()
		{
			super();
			
			_images = new ArrayCollection();
			_application = Application.application;
		}
		
		public function addImageObject(imageObject:ImageObject):void
		{
			this.addChild(imageObject);
			_images.addItem(imageObject);
		}
		
		public function alreadyLoaded(imageUrl:String):Boolean
		{
			for each(var imageObject:ImageObject in _images)
			{
				if(imageObject.imageUrl == imageUrl)
					return true;
			}
			return false;
		}
		
		public function getSelectedImageObjects():ArrayCollection
		{
			var selectedImages:ArrayCollection = new ArrayCollection();
			
			for each(var imageObject:ImageObject in _images)
			{
				if(imageObject.selected)
					selectedImages.addItem(imageObject);
			}
			
			return selectedImages;
		}
		
		public function deselectAllImageObjects():void
		{
				
			for each(var imageObject:ImageObject in _images)
			{
					if(imageObject.selected)
						imageObject.deselectImage();
			}
		}
		
		public function removeSelectedImageObjects():void
		{
			var images:ArrayCollection = getSelectedImageObjects();
			for each(var imageObject:ImageObject in images)
			{
				_images.removeItemAt(_images.getItemIndex(imageObject));
				removeChild(imageObject);
			}
		}
		
		public function showImageFullscreen(imageObject:ImageObject):void
		{
			_fullImageScreen = new FullImageScreen();
			_fullImageScreen.imageSource = imageObject.imageUrl;
			_application.addChild(_fullImageScreen);
			
			currentFullscreenImage = imageObject;
			_application.captureTagAreaInput = false;
			Application.application.currentState='fullscreen';
			
			// TODO: hide dock
			// Application.application.picpicDock.dock=false;
		}
		
		public function showImageSplitScreen(imageObject:ImageObject):void
		{
			var splitScreen:SplitImageScreen = new SplitImageScreen();
			splitScreen.leftImageUrl = lastSelectedImageUrl;
			splitScreen.rightImageUrl = imageObject.imageUrl;
			_application.addChild(splitScreen);
			
			_application.captureTagAreaInput = false;
		}
		
		public function loadNextImageForFullscreen():void
		{
			var index:int = _images.getItemIndex(currentFullscreenImage);
			index++;
			
			if(index < _images.length)
				loadImageForFullscreen(index);
		}
		
		public function loadPrevImageForFullscreen():void
		{
			var index:int = _images.getItemIndex(currentFullscreenImage);
			index--;
			
			if(index >= 0)
				loadImageForFullscreen(index);
		}
		
		public function loadImageForFullscreen(index:int):void
		{
			var image:ImageObject = _images.getItemAt(index) as ImageObject;
			currentFullscreenImage = image;
			_fullImageScreen.imageSource = image.imageUrl;
		}
	}
}